
$(function () {
    ListenerFundJZFind();
})

var timeoutId = 0;
function ListenerFundJZFind() {
    $("#fundName").bind("input propertychange", function() {
        QueryKeyword();
    });
}

function QueryKeyword() {
    clearTimeout(timeoutId);
    timeoutId = setTimeout(function () {
        var val = $("#fundName").val();
        console.log(val);
        $.ajax({
            url: "/web-liuxw/liuxw/searchFundName",
            dataType: "json",
            data: {"fundName": val},
            type: "post",
            success: function(res) {
                var fundNameArr = [];
                res.data.forEach(function (value) {
                    fundNameArr.push(value.CODE + ":" + value.NAME);
                });

                $("#fundName").autoComplete({source:fundNameArr});

            },
            error: function () {

            }
        })
    }, 3000);

}

var myDate = new Date();
var app3 = new Vue({
    el: "#app3",
    data: {
        nowDate: myDate.getFullYear()+"-"
        +(Array(2).join(0) + (myDate.getMonth() + 1)).slice(-2)+"-"
        +(Array(2).join(0) + myDate.getDate()).slice(-2)+" "
        +(Array(2).join(0) + myDate.getHours()).slice(-2)+":"
        +(Array(2).join(0) + myDate.getMinutes()).slice(-2)+":"
        +(Array(2).join(0) + myDate.getSeconds()).slice(-2)
    }
})

var app4 = new  Vue({
    el: "#app4",
    data: {items: ''},
    methods: {
        search: function () {
            this.$http.get(
                'liuxw/searchFund'
            ).then(// get data
                function(res) {
                    var dataArr = [];
                    res.body.data.forEach((item, index) => {
                    dataArr.push(JSON.parse(item));
                    });
                    this.items = dataArr;
                },
                // error callback
                function(error) {
                    alert("error");
                }
            );
        }
    }
})

var app2 = new Vue({
    el: "#app2",
    data: {
        items: '',
        itemArrays: [],
        itemArray: [],
        itemJson: ''
    },
    methods: {
        search: function () {
            this.$http.jsonp(
                'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&token=4f1862fc3b5e77c150a2b985b12db0fd&sty=FCRH&js=(%7Bdata%3A%5B(x)%5D%2CrecordsFiltered%3A(tot)%7D)&cmd=R.0000011%2C3990012%2C0003001%2C3990062%2C3990052%2CHSI5%2CHSCEI5%2CHSCCI5%7C_UI_MAP_ASIA&st=y&p=1&ps=8',
                {jsonp:'cb'}
            ).then(// get data
                function(res) {
                    this.itemArrays = [];
                    res.body.data.forEach(function (value) {
                        app2.itemArray = value.split(",");
                        app2.itemArrays.push(app2.itemArray);
                    })
                    this.items = this.itemArrays;

                    var tdata = []
                    app2.itemArrays.forEach(function (value) {
                        var tt ={
                            symbol: value[1],
                            name: value[2],
                            lastTrade: value[3],
                            change: value[4],
                            chg: value[5],
                            dateTime: value[6]
                        }
                        tdata.push(tt)
                    })

                    var Main = {
                        data() { return { tableData: tdata } }
                    }
                    var Ctor = Vue.extend(Main)
                    new Ctor().$mount('#app')
                },
                // error callback
                function(error) {
                    alert("error");
                }
            );
        }
    },
    mounted: function () {
        this.search();
        app4.search();
        var interval = setInterval(function(){
            var myDate = new Date();
            app3.nowDate =
                myDate.getFullYear()+"-"
                +(Array(2).join(0) + (myDate.getMonth() + 1)).slice(-2)+"-"
                +(Array(2).join(0) + myDate.getDate()).slice(-2)+" "
                +(Array(2).join(0) + myDate.getHours()).slice(-2)+":"
                +(Array(2).join(0) + myDate.getMinutes()).slice(-2)+":"
                +(Array(2).join(0) + myDate.getSeconds()).slice(-2);
            var sfm =
                (Array(2).join(0) + myDate.getHours()).slice(-2)
                +(Array(2).join(0) + myDate.getMinutes()).slice(-2)
                +(Array(2).join(0) + myDate.getSeconds()).slice(-2);;
            if(sfm >= '093000' && sfm <= '170200'){
                app2.search();
                app4.search();
            }
        }, 10000);
    }
});

